package huixue.cn.com.rxapp.adapter;

import android.content.ClipData;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.entity.Gank;

/**
 * Created by gongdongyang on 2018/7/17.
 */

public class IndexAdapter extends RecyclerView.Adapter<IndexAdapter.ComViewHolder> implements View.OnClickListener {
    private Context mContext;
    private List<Gank> mList;
    private OnItemClickListener mItemClickListener;


    public IndexAdapter(Context mContext, List<Gank> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public ComViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_item,parent,false);
        itemView.setOnClickListener(this);
        return new ComViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ComViewHolder holder, int position) {
        Uri uri = Uri.parse(mList.get(position).getUrl());
        holder.itemView.setTag(position);
        holder.img_image.setImageURI(uri);
        holder.tv_content.setText(mList.get(position).getDesc());
    }

    @Override
    public int getItemCount() {
        return (mList==null)?0:mList.size();
    }


    class ComViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_title;

        private TextView tv_content;

        private SimpleDraweeView img_image;

        //private View mItemView;

        ComViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            img_image = itemView.findViewById(R.id.img_image);
            tv_content =  itemView.findViewById(R.id.tv_content);
        }
    }

    public List<Gank> getmList() {
        return mList;
    }

    public void setmList(List<Gank> mList) {
        this.mList = mList;
    }


    public void setmItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position,View view);
    }

    @Override
    public void onClick(View v) {
        if (mItemClickListener!=null){
            mItemClickListener.onItemClick((Integer) v.getTag(),v);
        }
    }
}
